/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.cliente;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.cliente.*;
import logica.Constante;
/**
 *
 * @author davi
 */
public class AdicionaCliente implements logica.Logica{

    @Override
    public void executa(HttpServletRequest req, HttpServletResponse res) throws Exception {
        
        Cliente cliente = new Cliente();
        cliente.setNome(req.getParameter("nome"));
        String dt_nasc = req.getParameter("dt_nasc");
        
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        
        c.setTime(sdf.parse(dt_nasc));
        
        cliente.setDtNasc(new java.sql.Date(c.getTimeInMillis()));
        
        c = Calendar.getInstance();
        cliente.setDtCadastro( new java.sql.Date(c.getTimeInMillis()) );
        cliente.setDtAtualizacao(  new java.sql.Date(c.getTimeInMillis()) );
        cliente.setStatus(Constante.ATIVO);
        
        
        
        req.setAttribute("operacao", "salvar");
        try{
        ClienteDAO dao = new ClienteDAO();
        dao.salvar(cliente);
        }
        catch(Exception e ){
        
        e.printStackTrace();
        RequestDispatcher rd = req.getRequestDispatcher("erro.jsp");
        rd.forward(req, res);
        }
        
        req.setAttribute("cliente", cliente);
        RequestDispatcher rd = req.getRequestDispatcher("sucesso.jsp");
        rd.forward(req, res);
        
    }
    
}
